package com.taozi.netty.socketio.use.classroomchat.controller;


import lombok.Data;

import java.io.Serializable;

/**
 * 返回结果 对请求结果进行包装
 * @author   mingtao
 */
@Data
public class ResultInfo<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /** 状态码 **/
    private Integer code;
    /** 消息 **/
    private String msg;
    /** 数据对象 **/
    private T data;

    public ResultInfo(){}

    public ResultInfo(Integer code, String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回无数据的成功结果封装对象
     * @return ResultInfo
     */
    public static ResultInfo success() {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setResultCode(ResultCode.SUCCESS);
        return resultInfo;
    }

    /**
     * 返回带数据的成功结果封装对象
     * @param data 数据
     * @return ResultInfo
     */
    public static ResultInfo success(Object data) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setResultCode(ResultCode.SUCCESS);
        resultInfo.setData(data);
        return resultInfo;
    }

    /**
     * 返回无数据的失败结果封装对象
     * @param resultCode ResultCode
     * @return ResultInfo
     */
    public static ResultInfo failure(ResultCode resultCode) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setResultCode(resultCode);
        return resultInfo;
    }

    /**
     * 返回带数据的失败结果封装对象
     * @param resultCode ResultCode
     * @param data 数据
     */
    public static ResultInfo failure(ResultCode resultCode, Object data) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setResultCode(resultCode);
        resultInfo.setData(data);
        return resultInfo;
    }

    /**
     * 设置code和msg
     * @param code ResultCode
     */
    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }

}
